package com.example.mall.web.controller;

import com.example.mall.domain.UserLogins;
import com.example.mall.dto.UserLoginsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserLoginsQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IUserLoginsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userLogins")
public class UserLoginsController {

    @Autowired
    public IUserLoginsService userLoginsService;

    /**
     * 分页查询用户登录信息表信息
     * @param userLoginsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询用户登录信息表信息",notes = "分页查询用户登录信息表信息",httpMethod = "GET")
    public JSONResult queryPage(UserLoginsQuery userLoginsQuery){
        PageDTO<UserLoginsDto> userLoginsDtoPageDTO = userLoginsService.queryPage(userLoginsQuery);
        return JSONResult.success("查询成功",userLoginsDtoPageDTO);
    }

    /**
     * 根据id查询用户登录信息表信息
     * @param loginId
     * @return
     */
    @GetMapping("query/{loginId}")
    @ApiOperation(value = "根据id查询用户登录信息表信息",notes = "根据id查询用户登录信息表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("loginId")Long loginId){
        //1.根据id查询用户信息
        if (loginId == null){
            throw new GlobalCustomerException(ErrorEnumCode.LOGINID_NOT_ID);
        }
        UserLogins userLogins = userLoginsService.getById(loginId);
        return JSONResult.success("查询成功",userLogins);
    }

    /**
     * 根据id查询用户登录信息表信息
     * @param loginId
     * @return
     */
    @GetMapping("queryUserLoginsById/{roleId}")
    @ApiOperation(value = "根据id查询用户登录信息表信息",notes = "根据id查询用户登录信息表信息",httpMethod = "GET")
    public JSONResult queryUserLoginsById(@PathVariable("loginId")Long loginId){
        //1.根据id查询用户信息
        if (loginId == null){
            throw new GlobalCustomerException(ErrorEnumCode.LOGINID_NOT_ID);
        }
        UserLogins userLogins = userLoginsService.queryUserLoginsById(loginId);
        return JSONResult.success("查询成功",userLogins);
    }

    /**
     * 查询全部用户登录信息表信息
     * @param
     * @return
     */
    @GetMapping("queryUserLoginsList")
    @ApiOperation(value = "查询全部用户登录信息表信息",notes = "查询全部用户登录信息表信息",httpMethod = "GET")
    public JSONResult queryUserLoginsList(){
        List<UserLogins> userLogins = userLoginsService.queryUserLoginsList();
        return JSONResult.success("查询成功",userLogins);
    }

    /**
     * 新增用户登录信息表
     * @param userLoginsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增用户登录信息表",notes = "新增用户登录信息表",httpMethod = "POST")
    public JSONResult insert(@RequestBody UserLoginsDto userLoginsDto){
        userLoginsService.insert(userLoginsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改用户登录信息表信息
     * @param userLoginsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改用户登录信息表信息",notes = "修改用户登录信息表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody UserLoginsDto userLoginsDto){
        userLoginsService.update(userLoginsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除用户登录信息表信息
     * @param loginId
     * @return
     */
    @PostMapping("delete/{roleId}")
    @ApiOperation(value = "删除用户登录信息表信息",notes = "删除用户登录信息表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("loginId") Long loginId) {
        userLoginsService.delete(loginId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除用户登录信息表信息
     * @param loginIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除用户登录信息表信息",notes = "批量删除用户登录信息表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> loginIds) {
        userLoginsService.deleteBatch(loginIds);
        return JSONResult.success("批量删除成功");
    }
}
